<?php
namespace plugins\address\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\address\model\AddressModel;
use think\cache\driver\Redis;
use think\Db;
/**
 * 需求配置首页
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 地址首页
     */
    public function index()
    {
        return $this->fetch();
    }
    public function index_view()
    {
        return $this->fetch();
    }
    /**
     * 地址新增
     */
    public function add_page()
    {
        return $this->fetch();
    }
    /**
     * 地址编辑
     */
    public function edit_page()
    {
        $param = $this->request->param();
        $this->assign('cri_id', $param['cri_id']);
        return $this->fetch();
    }

    /**
     * 获取地区列表
     */
    public function getAddressList(){
        $param = $this->request->param();
        $addressModel = new AddressModel();
        $code_arr = [];
        if(!empty($param['cri_id'])){
            $address = $addressModel->where('cri_id',$param['cri_id'])->find();
            $cri_code = $address['cri_code'];
            $code_arr = $addressModel->getLowLevel($cri_code);
        }
        $da = $addressModel->field('cri_id,cri_code,cri_name,cri_lng,cri_lat,cri_superior_code')->order('cri_code asc')->select()->each(function ($item){
            $item['label'] = $item['cri_name'];
            $item['value'] = $item['cri_code'];
            return $item;
        })->toArray();
        $list = $addressModel->getTree($da,$code_arr);


        return api_json_echo(true,'查询成功',$list);
    }

    /**
     * 地区详情
     */
    public function getAddressDetail(){
        $param = $this->request->param();
        if(empty($param['cri_id'])){
            return api_json_echo(false, '传参错误');
        }
        $info = Db::name('address')->where('cri_id', $param['cri_id'])->find();
        return api_json_echo(true, '查询成功',$info);
    }


    /**
     * 添加下级地区操作
     */
    public function add()
    {
        $param = $this->request->param();
        if (empty($param['cri_superior_code']) || empty($param['cri_name']) || empty($param['cri_code'])) {
            return api_json_echo(false, '传参错误');
        }
        //获取数组最后一位
        $cri_superior_code = end($param['cri_superior_code']);
        $param['cri_superior_code'] = $cri_superior_code;


        //上级信息
        $fa_data = Db::name('address')->where('cri_code', $param['cri_superior_code'])->find();
        if (empty($fa_data)) {
            return api_json_echo(false, '查无上级信息');
        }
        $re_data = Db::name('address')->where('cri_code', $param['cri_code'])->find();
        if (!empty($re_data)) {
            return api_json_echo(false, '该地区代码已被使用');
        }
        $param['cri_level'] = $fa_data['cri_level']+1;
        $param['cri_gmt_create'] =  time(); //创建时间
        $param['status'] = 1; //状态
        $re = Db::name('address')->strict(false)->insert($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 修改信息操作
     */
    public function edit()
    {
        $param = $this->request->param();
        if (empty($param['cri_id']) || empty($param['cri_superior_code']) || empty($param['cri_name']) || empty($param['cri_code'])) {
            return api_json_echo(false, '传参错误');
        }
        if(is_array($param['cri_superior_code'])){
            //获取数组最后一位
            $cri_superior_code = end($param['cri_superior_code']);
            $param['cri_superior_code'] = $cri_superior_code;
        }
        //上级不能选择自己
        if($param['cri_code'] == $param['cri_superior_code']){
            return api_json_echo(false, '上级不能选当前地区');
        }

        $re_data = Db::name('address')->where('cri_code', $param['cri_code'])->find();
        if ($re_data) {
            if ($param['cri_id'] != $re_data['cri_id']) {
                return api_json_echo(false, '该地区代码已被使用');
            }
        }
        $param['cri_gmt_modified'] = time();
        $re = Db::name('address')->strict(false)->update($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }
    /**
     * 地区操作 --删除
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            return api_json_echo(false, '传值错误');
        }
        $cri_id = $param['cri_id'];
        //查询有无下级
        $address = Db::name('address')->where('cri_id', $cri_id)->find();
        $has_count = Db::name('address')->where('cri_superior_code',$address['cri_code'])->count();
        if($has_count>0){
            return api_json_echo(false, '请先删除关联的下级地区');
        }
        $re = Db::name('address')->where('cri_id', $cri_id)->delete();
        if (empty($re)) {
            return api_json_echo(false, '删除失败');
        }
        return api_json_echo(true, '删除成功');
    }

}
